import builtins

def EncodeNegBase(n, b):
    if n == 0:
        return '0'
    out = []
    while n != 0:
        n, rem = builtins.divmod(n, b)
        if rem < 0:
            n += 1
            rem -= b
        out.append(rem)
    return ''.join(builtins.map(builtins.str, out[::-1]))

def DecodeNegBase(nstr, b):
    if nstr == '0':
        return 0
    total = 0
    for i, ch in builtins.enumerate(nstr[::-1]):
        total += builtins.int(ch) * b ** i
    return total
if __name__ == '__main__':
    builtins.print('Encode 10 as negabinary (expect 11110)')
    result = EncodeNegBase(10, -2)
    builtins.print(result)
    if DecodeNegBase(result, -2) == 10:
        builtins.print('Converted back to decimal')
    else:
        builtins.print('Error converting back to decimal')
    builtins.print('Encode 146 as negaternary (expect 21102)')
    result = EncodeNegBase(146, -3)
    builtins.print(result)
    if DecodeNegBase(result, -3) == 146:
        builtins.print('Converted back to decimal')
    else:
        builtins.print('Error converting back to decimal')
    builtins.print('Encode 15 as negadecimal (expect 195)')
    result = EncodeNegBase(15, -10)
    builtins.print(result)
    if DecodeNegBase(result, -10) == 15:
        builtins.print('Converted back to decimal')
    else:
        builtins.print('Error converting back to decimal')